/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package AccessPermission;

import Module.ModuleBean;
import SubModule.SubModuleBean;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Vector;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author aamod
 */
public class UserAcessPermission extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try 
        {
          String id=request.getParameter("id");
          Vector<Object> list=AccessPermission.Query.viewAllModule();
          if(!list.isEmpty())
          {
             Iterator it=list.iterator();
             while(it.hasNext())
             {
                 ModuleBean mb=(ModuleBean)it.next();
                 Vector<Object> list1=AccessPermission.Query.viewAllSubModule(mb.getModuleId());
                 Vector<Object> vector=AccessPermission.Query.userAccessPermissionViewAll(Integer.parseInt(id),mb.getModuleId());
                 if(vector.size()!=0)
                 {
                 Vector<Object> vector1=AccessPermission.Query.userAccessPermissionView(Integer.parseInt(id),mb.getModuleId());
                 out.println("<legend>");
                 out.print(mb.getModuleName());
                 out.println("</legend>");
                 out.println("<div class='control-group'>");
                 out.println("<label for='check' class='control-label'><b>");
                 out.println(mb.getModuleName());
                 out.println("</b>&nbsp;&nbsp;");
                 if(list1.size()==vector.size())
                 {
                 out.println("<input type='checkbox' name='"+mb.getModuleName()+"' id='"+list1.size()+"/Head/"+mb.getModuleName()+"' onclick='checkUncheck(this.id)' value='"+mb.getModuleName()+"' checked>");
                 }
                 else
                 {
                  out.println("<input type='checkbox' name='"+mb.getModuleName()+"' id='"+list1.size()+"/Head/"+mb.getModuleName()+"' onclick='checkUncheck(this.id)' value='"+mb.getModuleName()+"'>");  
                 }
                 out.println("</label><div class='controls'>");
                 out.println("<label class='checkbox'></label>");
                 int i=1;
                 for(int j=0;j<list1.size();j++)
                 {
                     SubModuleBean sb=(SubModuleBean)list1.get(j);
                     UserAccessPermissionBean ub=(UserAccessPermissionBean)vector1.get(j);
                     ModuleBean mb1=sb.getModuleBean();
                     out.println("<label class='checkbox'>");
                     if(list1.size()==vector.size())
                     {
                     out.println("<input type='checkbox' name='"+mb1.getModuleName()+"' id='"+i+"/Child/"+mb1.getModuleName()+i+"' onclick='checkUncheck(this.id)' value='"+sb.getSubModuleName()+"' checked disabled>"+sb.getSubModuleName());
                     }
                     else
                     {
                         if(ub.getAcessStatus()==1)
                         {
                         out.println("<input type='checkbox' name='"+mb1.getModuleName()+"' id='"+i+"/Child/"+mb1.getModuleName()+i+"' onclick='checkUncheck(this.id)' value='"+sb.getSubModuleName()+"' checked>"+sb.getSubModuleName());
                         }
                         else
                         {
                          out.println("<input type='checkbox' name='"+mb1.getModuleName()+"' id='"+i+"/Child/"+mb1.getModuleName()+i+"' onclick='checkUncheck(this.id)' value='"+sb.getSubModuleName()+"'>"+sb.getSubModuleName());   
                         }
                     }
                     out.println("</label>");
                     i++;
                 }
                 out.println("</div>");
             }
             else
              {
                 out.println("<legend>");
                 out.print(mb.getModuleName());
                 out.println("</legend>");
                 out.println("<div class='control-group'>");
                 out.println("<label for='check' class='control-label'><b>");
                 out.println(mb.getModuleName());
                 out.println("</b>&nbsp;&nbsp;");
                 out.println("<input type='checkbox' name='"+mb.getModuleName()+"' id='"+list1.size()+"/Head/"+mb.getModuleName()+"' onclick='checkUncheck(this.id)' value='"+mb.getModuleName()+"'>");
                 out.println("</label><div class='controls'>");
                 out.println("<label class='checkbox'></label>");
                 int i=1;
                 for(int j=0;j<list1.size();j++)
                 {
                     SubModuleBean sb=(SubModuleBean)list1.get(j);
                     ModuleBean mb1=sb.getModuleBean();
                     out.println("<label class='checkbox'>");
                     out.println("<input type='checkbox' name='"+mb1.getModuleName()+"' id='"+i+"/Child/"+mb1.getModuleName()+i+"' onclick='checkUncheck(this.id)' value='"+sb.getSubModuleName()+"'>"+sb.getSubModuleName());
                     out.println("</label>");
                     i++;
                 }
                 out.println("</div>");
              }
             }
             out.println("</div>");
          }
        }
        catch(Exception e)
        {
            out.println(e);
        }
        finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
